/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 23735
 * Date: 2023-08-22
 * Time: 21:24
 */
public class Solution {
    public void gameOfLife(int[][] board) {
        int row = board.length;
        int col = board[0].length;
        // 周围 8 个位置
        int[][] next = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}, {-1, -1}, {-1, 1}, {1, -1}, {1, 1}};
        for (int i = 0; i < row; i++) {
            for (int j = 0; j < col; j++) {
                if (board[i][j] % 10 == 1) {
                    // 原本是活的细胞
                    for (int k = 0; k < 8; k++) {
                        // 注意 nextX/Y 的位置都是 基于 (i, j), nextX/Y 不要累加
                        int nextX = i + next[k][0];
                        int nextY = j + next[k][1];
                        if (nextX < 0 || nextX >= row || nextY < 0 || nextY >= col) {
                            // 越界了
                            continue;
                        }
                        // 活的细胞给周围细胞生命值 + 10
                        board[nextX][nextY] += 10;
                    }
                }
            }
        }
        for (int i = 0; i < row; i++) {
            for (int j = 0; j < col; j++) {
                // 周围活细胞的数目
                int environment = (board[i][j]/10) % 10;
                if (board[i][j] % 10 == 1) {
                    // 原本是活的细胞
                    if (environment == 2 || environment == 3) {
                        board[i][j] = 1;
                    } else {
                        board[i][j] = 0;
                    }
                } else {
                    if (environment == 3) {
                        board[i][j] = 1;
                    } else {
                        board[i][j] = 0;
                    }
                }
            }
        }
        return ;
    }
}
